package com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.AddChapter.Adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tnot.R;
import com.example.tnot.Teacher.CoursePart.Container.AddCourse.ItemBean.Term;
import com.example.tnot.Teacher.CoursePart.CourseInternal.ClassDetail.VpFragment.AddChapter.ItemBean.ClassItem;
import com.example.tnot.Util.ToastUtil;

import org.w3c.dom.Text;

import java.util.List;

public class SelectClassAdapter extends RecyclerView.Adapter<SelectClassAdapter.ViewHolder>{
    private List<ClassItem> classList;
    private int selectedPosition = -1;
    private int clickedPosition = -1;
    public SelectClassAdapter(List<ClassItem> classList) {
        this.classList = classList;
    }

    @NonNull
    @Override
    public SelectClassAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_select_class, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SelectClassAdapter.ViewHolder holder, int position) {
        ClassItem item = classList.get(position);
        //设置班级
        holder.tv_class.setText(classList.get(position).getClassName());

        holder.rb_select.setChecked(item.isSelected());
        holder.rb_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickedPosition = holder.getBindingAdapterPosition();
//                Log.d("clickedPosition", String.valueOf(clickedPosition));
                // 如果点击的是已经选中的项，则取消选中
                if (clickedPosition == selectedPosition) {
                    item.setSelected(false);
                    selectedPosition = -1;
                    notifyItemChanged(clickedPosition);
                } else {
                    // 取消之前选中的项
                    if (selectedPosition >= 0 && selectedPosition < classList.size()) {
                        classList.get(selectedPosition).setSelected(false);
                        notifyItemChanged(selectedPosition);
                    }

                    // 选中当前项
                    item.setSelected(true);
                    selectedPosition = clickedPosition;
                    notifyItemChanged(selectedPosition);
                }
//                ToastUtil.showCustomToast(v.getContext(), "已选择" + item.getClassName()+",boolean_is_check:"+item.isSelected());
            }
        });
    }

    @Override
    public int getItemCount() {
        return classList.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_class;
        RadioButton rb_select;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_class = itemView.findViewById(R.id.tv_class);
            rb_select = itemView.findViewById(R.id.rb_select);
        }
    }
    public String getSelectedClass() {
        if (selectedPosition >= 0 && selectedPosition < classList.size()) {
            return classList.get(selectedPosition).getClassName();
        }
        return null;
    }
}
